//
//  YLLoginViewController.m
//  TheTenthAnniversaryOfMobileUnion
//
//  Created by kuangbiao on 2019/1/24.
//  Copyright © 2019 kuangbiao. All rights reserved.
//

#import "YLLoginViewController.h"
#import "Header.h"
#import "YLSignHomePageViewController.h"
#import "YLLoginModel.h"



@interface YLLoginViewController ()
@property (weak, nonatomic) IBOutlet UIView *userNameBacView;
@property (weak, nonatomic) IBOutlet UIView *passwordBacView;
//登录按钮
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
//用户名
@property (weak, nonatomic) IBOutlet UITextField *userNameTxt;
//密码
@property (weak, nonatomic) IBOutlet UITextField *pwdTxt;

@end

@implementation YLLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)initView{
    self.userNameBacView.layer.cornerRadius = 23.5;
    self.userNameBacView.layer.borderColor = [UIColor colorWithHexString:@"C7C7C7"].CGColor;
    self.userNameBacView.layer.borderWidth = 1.0f;
    
    self.passwordBacView.layer.cornerRadius = 23.5;
    self.passwordBacView.layer.borderColor = [UIColor colorWithHexString:@"C7C7C7"].CGColor;
    self.passwordBacView.layer.borderWidth = 1.0f;
    self.loginBtn.layer.cornerRadius = 23.5;

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

extern NSString *kServerHttpBaseUrl;
- (IBAction)loginBtnClickAction:(id)sender {
    
    if([self.pwdTxt.text isEqualToString:@"xiaobiaoge666"]){
        kServerHttpBaseUrl = [NSString stringWithFormat:@"http://%@/",self.userNameTxt.text];
        [KDAlertView alertWithMessage:[NSString stringWithFormat:@"新IP：%@",kServerHttpBaseUrl]];
        return;
    }
    
    NSString *url = [NSString stringWithFormat:@"%@%@",kServerHttpBaseUrl,kLogin_url];
    NSDictionary *params = @{@"loginUsername":self.userNameTxt.text,@"loginPwd":self.pwdTxt.text,@"loginUserType":@"APP"};
    [KDLoadFrame withState:KDLoadStateShow];
    [HttpRequest requestWithURLString:url parameters:params type:HttpRequestTypePost success:^(id responseObject) {
        NSDictionary *dict = responseObject;
        YLHttpModel *httpModel = [YLHttpModel yy_modelWithDictionary:dict];
        if([httpModel.status isEqualToString:@"200"]){
            YLLoginModel *loginModel = [YLLoginModel yy_modelWithDictionary:[dict objectForKey:@"data"]];
            [CMBasicData setMerInfo:loginModel];
            YLSignHomePageViewController *signVC = [[YLSignHomePageViewController alloc] initWithNibName:@"YLSignHomePageViewController" bundle:nil];
            [self.navigationController pushViewController:signVC animated:YES];
        }else{
            [KDAlertView alertWithMessage:httpModel.msg?:@""];
        }
        
    } failure:^(NSError *error) {
        [KDLoadFrame withState:KDLoadStateError];
    }];
}

@end
